package org.apache.catalina.startup;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Objects;
import java.util.Properties;

/**
 * CatalinaProperties conf/catalina.properties
 *
 * @author chentudong
 * @date 2024/2/19 18:29
 * @since 1.0
 */
public class CatalinaProperties {

  private static Properties properties = null;

  static {
    loadProperties();
  }

  /**
   * @param name The property name
   * @return specified property value
   */
  public static String getProperty(String name) {
    return properties.getProperty(name);
  }

  /**
   * loadProperties
   */
  public static void loadProperties() {
    InputStream is = null;
    try {
      //拿到catalina base目录
      File home = new File(Bootstrap.getCatalinaBase());
      //拼接目录，定位到conf目录
      File conf = new File(home, "conf");
      //拼接文件，定位../catalina.properties文件
      File propsFile = new File(conf, "catalina.properties");
      is = Files.newInputStream(propsFile.toPath());
    } catch (Throwable t) {
    }
    if (Objects.nonNull(is)) {
      try {
        properties = new Properties();
        properties.load(is);
      } catch (Throwable t) {

      } finally {
        try {
          is.close();
        } catch (Exception e) {

        }
      }
    }
  }
}
